package com.my.control;

import java.util.List;

import com.my.bean.Admin;
import com.my.bean.User;
import com.my.util.PageBean;

public class AccountAction extends BaseAction {

	private String newpassword;
	private String oldAdminName;

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public String getOldAdminName() {
		return oldAdminName;
	}

	public void setOldAdminName(String oldAdminName) {
		this.oldAdminName = oldAdminName;
	}

	public String login() throws Exception {
		if (admin.getName().isEmpty() || admin.getPass().isEmpty()) {
			request.put("message",
					"<script laguage='JavaScript'> alert('用户名密码不能为空!') </script>");
			return "login";
		}
		Admin adm = accountbo.checkLoginAdmin(admin);
		if (adm == null) {
			request.put("message",
					"<script laguage='JavaScript'> alert('用户名密码不正确!') </script>");
			return "login";
		}
		StringBuilder stf = new StringBuilder();
		if (adm.getRole().getId() == 1) {
			stf.append("<H1 class='title'><A href='javascript:void(0)'><font size='2' color=#313196>管理用户信息</font></a></H1>");
			stf.append("<div class='content'>");
			stf.append("<p>&nbsp;&nbsp;<a href='addadmin.jsp' target='main'><font size='2' color=#5454c6>添加用户</font></a></p>");
			stf.append("<p>&nbsp;&nbsp;<a href='listadmin.jsp' target='main'><font size='2' color=#5454c6>用户信息列表</font></a></p>");
			stf.append("</div>");

			stf.append("<H1 class='title'><A href='javascript:void(0)'><font size='2' color=#313196>管理维修点信息</font></a></H1>");
			stf.append("<div class='content'>");
			stf.append("<p>&nbsp;&nbsp;<a href='addaddress.jsp' target='main'><font size='2' color=#5454c6>添加维修点</font></a></p>");
			stf.append("<p>&nbsp;&nbsp;<a href='listaddress.jsp' target='main'><font size='2' color=#5454c6>维修点信息列表</font></a></p>");
			stf.append("</div>");

			stf.append("<H1 class='title'><A href='javascript:void(0)'><font size='2' color=#313196>管理配件信息</font></a></H1>");
			stf.append("<div class='content'>");
			stf.append("<p>&nbsp;&nbsp;<a href='addpart.jsp' target='main'><font size='2' color=#5454c6>添加配件</font></a></p>");
			stf.append("<p>&nbsp;&nbsp;<a href='listpart.jsp' target='main'><font size='2' color=#5454c6>配件信息列表</font></a></p>");
			stf.append("</div>");
			session.put("rights", stf.toString());
			session.put("admin", adm);
			return "main";
		} else {
			stf.append("<H1 class='title'><A href='javascript:void(0)'><font size='2' color=#313196>用户信息一览</font></a></H1>");
			stf.append("<div class='content'>");
			stf.append("<p>&nbsp;&nbsp;<a href='listuser.jsp' target='main'><font size='2' color=#5454c6>用户信息列表</font></a></p>");
			stf.append("</div>");

			stf.append("<H1 class='title'><A href='javascript:void(0)'><font size='2' color=#313196>报修信息管理</font></a></H1>");
			stf.append("<div class='content'>");
			stf.append("<p>&nbsp;&nbsp;<a href='listorder.jsp' target='main'><font size='2' color=#5454c6>未受理</font></a></p>");
			stf.append("<p>&nbsp;&nbsp;<a href='listorder2.jsp' target='main'><font size='2' color=#5454c6>已受理</font></a></p>");
			stf.append("</div>");

			stf.append("<H1 class='title'><A href='javascript:void(0)'><font size='2' color=#313196>反馈信息管理</font></a></H1>");
			stf.append("<div class='content'>");
			stf.append("<p>&nbsp;&nbsp;<a href='listorder3.jsp' target='main'><font size='2' color=#5454c6>未反馈</font></a></p>");
			stf.append("<p>&nbsp;&nbsp;<a href='listorder4.jsp' target='main'><font size='2' color=#5454c6>已反馈</font></a></p>");
			stf.append("</div>");
			session.put("rights", stf.toString());
			session.put("admin", adm);
			return "main";
		}
	}

	public String logout() throws Exception {
		request.clear();
		session.clear();
		return "login";
	}

	public String editAdminPass() throws Exception {
		Admin adm = (Admin) session.get("admin");
		admin.setName(adm.getName());
		adm = accountbo.checkLoginAdmin(admin);
		if (adm == null) {
			request.put("message",
					"<script laguage='JavaScript'> alert('原始密码不对，请重新输入!') </script>");
			return "editadminpass";
		}
		adm.setPass(newpassword);
		accountbo.editAdmin(adm);
		session.clear();
		request.put("message",
				"<script laguage='JavaScript'> alert('修改成功,请重新登录!') </script>");
		return "editadminpass";
	}

	public String listUserPrepare() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<User> list;
		list = accountbo.getAllUserByPage(pageNo, 15);
		totalRecords = accountbo.getTotalUserRecords().intValue();
		PageBean<User> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listUserPrepare_acc");
		return "listuser";
	}

	public String searchUser() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<User> list;
		list = accountbo.searchUserByPage(pageNo, 15, user);
		totalRecords = accountbo.searchTotalUserRecords(user).intValue();
		PageBean<User> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchUser_acc");
		return "listuser";
	}

	public String addAdminPrepare() throws Exception {
		request.put("roleList", accountbo.getRoleList());
		return "addadmin";
	}

	public String addAdmin() throws Exception {
		String adminName = admin.getName();
		if (adminName.isEmpty()) {
			request.put("message", "用户名不能为空!");
		} else if (accountbo.isExistAdmin(adminName)) {
			request.put("message", "用户名已经存在!");
		} else if (accountbo.addAdmin(admin)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('添加成功!') </script>");
			return this.listAdminPrepare();
		} else {
			request.put("message", "添加失败!");
		}
		return this.addAdminPrepare();
	}

	public String listAdminPrepare() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Admin> list;
		list = accountbo.getAllAdminByPage(pageNo, 15);
		totalRecords = accountbo.getTotalAdminRecords().intValue();
		PageBean<Admin> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listAdminPrepare_acc");
		return "listadmin";
	}

	public String searchAdmin() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Admin> list;
		list = accountbo.searchAdminByPage(pageNo, 15, admin);
		totalRecords = accountbo.searchTotalAdminRecords(admin).intValue();
		PageBean<Admin> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchAdmin_acc");
		return "listadmin";
	}

	public String removeAdmin() throws Exception {
		accountbo.removeAdmin(admin.getId());
		return this.listAdminPrepare();
	}

	public String editAdminPrepare() throws Exception {
		admin = accountbo.getAdminById(Admin.class, admin.getId());
		request.put("admin", admin);
		request.put("roleList", accountbo.getRoleList());
		return "editadmin";
	}

	public String editAdmin() throws Exception {
		String adminName = admin.getName();
		if (adminName.isEmpty()) {
			request.put("message", "用户名不能为空!");
			return this.editAdminPrepare();
		} else if (!adminName.equals(oldAdminName)) {
			if (accountbo.isExistAdmin(adminName)) {
				request.put("message", "用户名已经存在!");
				return this.editAdminPrepare();
			}
		}
		if (accountbo.editAdmin(admin)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('修改成功!') </script>");
			return this.listAdminPrepare();
		} else {
			request.put("message", "修改失败!");
		}
		return this.listAdminPrepare();
	}
}
